// noinspection JSUnusedGlobalSymbols

import { ObjectTool } from './ObjectTool'

/**
 * 常量值映射
 */
export class ConstantValueMapping<T extends Record<string, PropertyKey> = Record<string, PropertyKey>> {
    /**
     * 原常量值, 保留
     */
    public constant: T

    /**
     * 选项数组数据
     */
    public dicArr: Array<{ label: keyof T, value: T[keyof T] }>

    /**
     * 展示用数据
     * @see toDisplay
     */
    public display: Record<T[keyof T], keyof T>

    /**
     */
    public constructor(constantValue: T = <T>{}) {
        this.constant = constantValue
        this.dicArr = ObjectTool.toArray(constantValue)
            .map(({ key, value }) => ({ label: key, value }))
        this.display = ObjectTool.keyValueReverse(constantValue)
    }

    /**
     * {@link #display} 的替代品, 设置默认值
     */
    public toDisplay(value: T[keyof T], defaultValue = '') {
        return this.display[value] ?? defaultValue
    }

    /**
     * 选项数组, 排除指定的值
     */
    public optionArrayExclude(...excludeValue: Array<T[keyof T]>) {
        return this.dicArr.filter(i => !excludeValue.includes(i.value))
    }

    /**
     * 选项数组 仅包含指定的值
     */
    public optionArrayInclude(...includeValue: Array<T[keyof T]>) {
        return this.dicArr.filter(i => includeValue.includes(i.value))
    }

}
